using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;
using ScriptManager.Data.ManagerObjects;
using ScriptManager.Data.BusinessObjects;
using ScriptManager.Data.Base;

namespace ScriptManager.Data.UnitTests
{
	[TestFixture]
    public partial class LineItemTests : UNuitTestBase
    {
        protected ILineItemManager manager;
        protected IOrderManager orderManager;
		
		public LineItemTests()
        {
            manager = managerFactory.GetLineItemManager();
            orderManager = managerFactory.GetOrderManager();
        }

        protected Order CreateNewOrder()
        {
            Order entity = new Order();


            entity.UserId = "Test ";
            entity.OrderDate = DateTime.Now;
            entity.ShipAddr1 = "Test Test Test Test Test T";
            entity.ShipAddr2 = "Test Test Test Test Test Test Test Test Test Test Test Test Test Test";
            entity.ShipCity = "Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test";
            entity.ShipState = "Test Test Test Test Test T";
            entity.ShipZip = "Test Test Test T";
            entity.ShipCountry = "T";
            entity.BillAddr1 = "Test Test Test Test Test Test Test Test Test ";
            entity.BillAddr2 = "Test Test Test Test Test Test Test Test Test Test Test";
            entity.BillCity = "Test Test Test Test ";
            entity.BillState = "Test Test Test Test Test Test Test Test Test Test Test Test Test Test Test ";
            entity.BillZip = "Test ";
            entity.BillCountry = "Test Test Te";
            entity.Courier = "Test Te";
            entity.TotalPrice = 13;
            entity.BillToFirstName = "Test Test Test Test Test Test Test Test Test Tes";
            entity.BillToLastName = "Test Test Test Test";
            entity.ShipToFirstName = "Test Test Test Test Test Test Test Test Test Test Test Test Test Test";
            entity.ShipToLastName = "Test Test Te";
            entity.AuthorizationNumber = 43;
            entity.Locale = "T";

            return entity;
        }

		protected LineItem CreateNewLineItem()
		{
			LineItem entity = new LineItem();
			
			entity.OrderId = 72;
			entity.LineNum = 67;
			entity.ItemId = "Test Te";
			entity.Quantity = 19;
			entity.UnitPrice = 89;
			
			return entity;
		}
		protected LineItem GetFirstLineItem()
        {
            IList<LineItem> entityList = manager.GetAll(1);
            if (entityList.Count == 0)
                Assert.Fail("All tables must have at least one row for unit tests to succeed.");
            return entityList[0];
        }
		
		[Test]
        public void Create()
        {
            try
            {
				LineItem entity = CreateNewLineItem();
				
                object result = manager.Save(entity);

                Assert.IsNotNull(result);
            }
            catch(Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }

        [Test]
        public void Read()
        {
            try
            {
                LineItem entityA = CreateNewLineItem();
				manager.Save(entityA);

                LineItem entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
		[Test]
		public void Update()
        {
            try
            {
                LineItem entityA = GetFirstLineItem();
				
				entityA.ItemId = "Test T";
				
				manager.Update(entityA);

                LineItem entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA.OrderId, entityB.OrderId);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }
        [Test]
        public void Delete()
        {
            try
            {
                LineItem entity = GetFirstLineItem();
				
                manager.Delete(entity);

                entity = manager.GetById(entity.Id);
                Assert.IsNull(entity);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }

        #region OtherTests

        /* This test will fail if the entity does not match an orderID as it is a FK as well as the PK
         * for the Line Item, the test below creates Order (This test is based on the assumption that
         * the order create and read test passes, if they fail, then the test should also fail
         * */

        [Test]
        public void ReadOrderCreated()
        {
            try
            {
                Order order = CreateNewOrder();
                orderManager.Save(order);

                LineItem entityA = CreateNewLineItem();
                entityA.OrderId = order.Id;
                manager.Save(entityA);

                LineItem entityB = manager.GetById(entityA.Id);

                Assert.AreEqual(entityA, entityB);
            }
            catch (Exception ex)
            {
                Assert.Fail(ex.ToString());
            }
        }

        #endregion
    }
}

